//
//  YFWeiboStatusViewModel.swift
//  YFSwiftWeibo
//
//  Created by tsaievan on 2017/9/7.
//  Copyright © 2017年 tsaievan. All rights reserved.
//

import UIKit

/// 单条微博的视图模型

/**
 * 如果是没有继承NSObject, 又想重写description方法, 必须遵守一个协议
 * CustomStringConvertible <=== 就是这个协议了
 * 然后再实现协议的description方法即可
 */
class YFWeiboStatusViewModel: CustomStringConvertible {
    
    var status: YFWeiboStatus
    
    var memberIcon: UIImage?
    
    var titleIcon: UIImage?
    
    var retweetStr: String?
    
    var commentStr: String?
    
    var likeStr: String?
    
    var picViewSize = CGSize()
    
    ///< 计算型属性
    ///< 转发微博没有图片
    ///< 原创微博有图片
    ///< 如果原创微博为空, 则返回原创的图片
    ///< 如果都没有, 则返回nil
    var picUrls: [YFWeiboPicModel]? {
        return status.retweeted_status?.pic_urls ?? status.pic_urls
    }
    
    ///< 转发微博的文字
    var retweetedText: String?
    
    ///< 行高
    var rowHeight: CGFloat = 0
    
    init(model: YFWeiboStatus) {
        status = model
        if let user = model.user {
            if user.mbrank > 0 && user.mbrank < 7 {
                let imageName = "common_icon_membership_level\(user.mbrank)"
                memberIcon = UIImage(named: imageName)
            }
            
            /**
             * /// 认证类型, -1: 没有认证, 0, 认证用户, 2,3,5: 企业认证, 220: 达人
             */
            switch user.verified_type {
            case 0:
                titleIcon = #imageLiteral(resourceName: "avatar_vip")
            case 2, 3, 5:
                titleIcon = #imageLiteral(resourceName: "avatar_enterprise_vip")
            case 220:
                titleIcon = #imageLiteral(resourceName: "avatar_grassroot")
            default: break
            }
        }
        ///< 测试代码, 测试转发数超过一万时的情况
        //        status.reposts_count = Int(arc4random_uniform(100000))
        retweetStr = countString(count: status.reposts_count, defaultString: "转发")
        commentStr = countString(count: status.comments_count, defaultString: "评论")
        likeStr = countString(count: status.attitudes_count, defaultString: "赞")
        picViewSize = calcPicViewSize(count: picUrls?.count)
        retweetedText = "@" + (status.user?.screen_name ?? "") + ":" + (status.retweeted_status?.text ?? "")
        calcRowHeight()
    }
    
    var description: String {
        return status.yy_modelDescription()
    }
}

extension YFWeiboStatusViewModel {
    fileprivate func countString(count: Int, defaultString: String)->String {
        if count == 0 {
            return defaultString
        }
        
        if count < 10000 {
            return count.description
        }
        
        if count >= 10000 {
            return String(format: "%.02f 万", Double(count) / 10000)
        }
        return defaultString
    }
    
    /// 根据配图数量计算picView的大小
    ///
    /// - Parameter count: 配图的数量
    /// - Returns: picView的大小
    fileprivate func calcPicViewSize(count: Int?)->CGSize {
        if count == 0 || count == nil {
            return CGSize()
        }
        ///< 1. 先准备两个常量
        ///< 2. 计算picView的宽度
        ///< 3. 计算picView的高度
        ///< 3.1 计算行数
        let row = (count! - 1) / 3 + 1
        ///< 3.2 计算每个Item的宽度, 高度(相等)
        ///< 3.3 计算picView的高度
        let height = CGFloat(row) * YFWeiboPicViewItemWidth + YFWeiboPicViewOutterMargin + CGFloat(row - 1) * YFWeiboPicViewInnerMargin
        return CGSize(width: YFWeiboPicViewWidth, height: height)
    }
    
    fileprivate func calcRowHeight() {
        /**
         * 原创微博:
         * margin(12)-iconImage(34)-margin(12)-正文label(需要计算15号字)-imageView的高度(需要计算)-margin(12)-toolbar工具条(35)
         
         * 转发微博:
         * margin(12)-iconImage(34)-margin(12)-正文label(需要计算15号字)-margin(12)-转发label(需要计算14号字)-imageView的高度(需要计算)-margin(12)-toolbar工具条(35)
         */
        let margin = YFWeiboPicViewOutterMargin
        let iconHeight: CGFloat = 34
        let toolbarHeight: CGFloat = 35
        let seperatorHeight: CGFloat = 8
        
        let originalFont = UIFont.systemFont(ofSize: 15)
        let retweetedFont = UIFont.systemFont(ofSize: 14)
        
        var height: CGFloat = 0
        height += seperatorHeight
        height += margin
        height += iconHeight
        height += margin
        
        let textSize = CGSize(width: UIScreen.hm_screenWidth() - 2 * margin, height: CGFloat(MAXFLOAT))
        let retweetedTextSize = CGSize(width: UIScreen.hm_screenWidth() - 4 * margin, height: CGFloat(MAXFLOAT))
        
        if let text = status.text {
            height += (text as NSString).boundingRect(with: textSize, options: [.usesLineFragmentOrigin], attributes: [NSFontAttributeName : originalFont], context: nil).height
        }
        
        ///< 判断是不是转发微博
        if status.retweeted_status != nil {
            height += margin
            if let retweetedText = status.retweeted_status?.text {
                height += (retweetedText as NSString).boundingRect(with: retweetedTextSize, options: [.usesLineFragmentOrigin], attributes: [NSFontAttributeName : retweetedFont], context: nil).height
            }
        }
        height += picViewSize.height
        height += margin
        height += toolbarHeight
        rowHeight = height
    }
    
    func calcImageSize(image: UIImage) {
        var size = image.size
        size.height += YFWeiboPicViewOutterMargin
        picViewSize = size
        
        ///< 这个地方一定要重新计算一下高度, 因为这里的picViewSize高度是改变了的,所以整个cell的高度也改变了
        calcRowHeight()
    }
}
